package cn.itcast.listener;

import cn.itcast.common.utils.SmsUtils;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;

import java.util.Map;

public class SmsListener implements MessageListener {

    @Override
    public void onMessage(Message message) {
        try {
            //1.获取数据消息：byte数组
            byte[] body = message.getBody();
            //2.将byte数组转化为json数据, 将json转化为java对象
            Map map = JSON.parseObject(body, Map.class);
            String telephone = (String) map.get("telephone");
            String msg = (String) map.get("message");
            SendSmsResponse sendSmsResponse = SmsUtils.sendCode(telephone, msg);
            if ("OK".equalsIgnoreCase(sendSmsResponse.getCode())) {
                System.out.println("发送成功");
            }else{
                System.out.println("完蛋了");
            }

        } catch (ClientException e) {
            e.printStackTrace();
            System.out.println("失败");
        }
    }
}
